package com.cn.gmall.weball.controller;

import com.cn.gmall.activity.client.ActivityFeign;
import com.cn.gmall.common.result.Result;
import com.cn.gmall.model.activity.SeckillGoods;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
public class SeckillController {
    @Resource
    private ActivityFeign activityFeign;



    /**
     * 确认订单
     * @param model
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String trade(Model model){
        Result<Map<String, Object>> result = activityFeign.trade();

        model.addAllAttributes(result.getData());

        return "seckill/trade";
    }


    /**
     * 排队控制器 点击秒杀后 跳转一个秒杀等待页面
     * 通过HttpServletRequest域来传输参数
     * @param skuId
     * @param skuIdStr
     * @param request
     * @return
     */

    @GetMapping("seckill/queue.html")
    public  String queue(@RequestParam("skuId") Long skuId,
                         @RequestParam("skuIdStr") String skuIdStr,
                         HttpServletRequest request){
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);
     return "seckill/queue";

    }

    /**
     * 秒杀详情页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String seckillGoodsItem(@PathVariable Long skuId, Model model) {
        Result result = activityFeign.getSeckillGoodsBySkuId(skuId);


        model.addAttribute("item", result.getData());

        return "seckill/item";
    }


    /***
     * 秒杀列表
     * @return
     */
    @GetMapping("/seckill.html")
    public String seckillIndex(Model model) {
     /*   List<SeckillGoods> data = (List<SeckillGoods>) activityFeign.findAll().getData();
        model.addAttribute("list", data);
*/
        Result<List<SeckillGoods>> all = activityFeign.findAll();
        model.addAttribute("list", all.getData());
        return "seckill/index";
    }


}
